package org.ben.birthday.twentybirthday.controller.administrator;

import org.ben.birthday.twentybirthday.entity.BackGroundImage;
import org.ben.birthday.twentybirthday.utils.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2019/9/4 17:33
 */
@Controller
@RequestMapping("/administrator")
public class AdministratorBackGroundImageController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdministratorBackGroundImageController.class);

    @RequestMapping("/insertBackGroundImage")
    public ModelAndView insertBackGroundImage(@Valid BackGroundImage backGroundImage,BindingResult bindingResult){
        ModelAndView modelAndView=new ModelAndView();
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            logger.error("注册失败！");
            for (FieldError fieldError : errors) {
                logger.error("错误的字段名：" + fieldError.getField());
                logger.error("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            modelAndView.setViewName("travel/userPage/travelRegist");
            return modelAndView;
        }
   boolean judgment=iBackGroundImageService.insertBackGroundImageService(backGroundImage);
    if (judgment){
        logger.info("添加成功");
        modelAndView.setViewName("");
        return modelAndView;
    }else {
        logger.error("添加失败");
        modelAndView.setViewName("");
        return modelAndView;
    }
    }

}
